/**
 * 
 */
package com.effectiv.req1.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.effectiv.req1.business.api.ProjectService;
import com.effectiv.req1.business.api.UserService;
import com.effectiv.req1.domain.Project;

import com.effectiv.req1.domain.User;
import com.effectiv.req1.repository.LovRepository;


/**
 * @author Dhrubo
 * 
 */
@Controller
@Slf4j
public class ProjectUserController {

	@Autowired
	private UserService userService;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private LovRepository lovRepository;

	@RequestMapping(value = "/newprojectuser")
	public final String addprojectuser(@RequestParam(value="userpresent", required=false, defaultValue = "false") boolean userPresent,
			@RequestParam(value="usersaved", required=false, defaultValue = "false") boolean userSaved,
			@RequestParam("project") String projectId, Model model) {

		model.addAttribute("users", userService.findUsersByCompany(null));
		model.addAttribute(projectService.findOne(projectId));
		model.addAttribute("userPresent", userPresent);
		model.addAttribute("userSaved", userSaved);
		model.addAttribute("projectuser", true);
		
		return "newprojectuser";
	}

	@RequestMapping(value = "/saveprojectuser")
	public final String saveProjectUser(@RequestParam("user") String userId,
			@RequestParam("project") String projectId, Model model) {

		Project project = projectService.addUser(projectId, userId);
		
		String url = "redirect:newprojectuser?project="+projectId;
		
		if(project == null){
			url = url + "&userpresent=true";
		}
		else{
			url = url + "&usersaved=true";
		}
			

		return url;
	}

}
